<template>
    <div>
        <el-form ref="form" :model="form" label-width="80px">
            <el-form-item label="父ID">
                <el-input v-model="pigInfo.fatherId"></el-input>
            </el-form-item>
            <el-form-item label="母ID">
                <el-input v-model="pigInfo.motherId"></el-input>
            </el-form-item>
            <el-form-item label="性别">
                <el-radio-group v-model="pigInfo.sex">
                    <el-radio label="1" value="1" name="sex">雄</el-radio>
                    <el-radio label="0" value="0" name="sex">雌</el-radio>
                </el-radio-group>
            </el-form-item>
<!--            <el-form-item label="猪类型" >-->
<!--                <el-select placeholder="请选择猪类型" v-model="pigInfo.type">-->
<!--                    <el-option v-for="pigType in pigTypeList" v-bind:label="pigType.typeName" v-bind:value="pigType.id"></el-option>-->
<!--                </el-select>-->
<!--            </el-form-item>-->
            <el-form-item label="生日">
                <el-col :span="11">
                    <el-date-picker value-format="yyyy-MM-dd" type="date" placeholder="选择日期" v-model="pigInfo.birthday" style="width: 100%;"></el-date-picker>
                </el-col>
<!--                <el-col class="line" :span="2">-</el-col>-->
<!--                <el-col :span="11">-->
<!--                    <el-time-picker placeholder="选择时间" v-model="form.date2" style="width: 100%;"></el-time-picker>-->
<!--                </el-col>-->
            </el-form-item>
            <label>猪窝选择</label>
            <el-tree
                    :data="piggeryTree"
                    show-checkbox
                    accordion
                    node-key="id"
                    @check="handleClick"
                    >
            </el-tree>

            <el-form-item style="position: relative;left: 35%;margin: 50px">
                <el-button type="primary" @click="onSubmit" >录入</el-button>
                <el-button>取消</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import pigApi from '../../../api/pigApi';
    export default {
        name: 'PigInfoInsert',
        created() {
            this.getPigTypeList();
            this.getPiggeryTree();
        },
        data() {
            return {
                //猪窝的tree
                piggeryTree: [],
                //猪的类型存储
                pigTypeList:[],
                //猪的信息记录
                pigInfo:{
                    birthday:""
                },
                form: {
                    name: '',
                    region: '',
                    date1: '',
                    date2: '',
                    delivery: false,
                    type: [],
                    resource: '',
                    desc: ''
                }
            }
        },
        methods: {
            //获取猪类型列表
            getPigTypeList(){
              pigApi.getPigTypeList()
                    .then(res=>{
                        this.pigTypeList = res.data.pigType
                    })
            },
            //猪舍列表
            getPiggeryTree(){
                pigApi.getPiggeryTree()
                    .then(res=>{
                        this.piggeryTree = res.data.piggeryTree
                    })
            },
            onSubmit() {
                console.log(this.pigInfo);
            },
            handleClick(data, node) {
                // console.log('data:',  data,'\n' ,'node:', node, '\n');
                // this.pigInfo.piggeryId = node.
                if(node.checkedNodes.length){
                    this.pigInfo.piggeryId = node.checkedNodes[0].id
                    // console.log(node.checkedNodes[0].id);
                }
                let parent = []
                this.piggeryTree.forEach(pigger=>{
                    let parentLock = false
                    pigger.children.forEach(piggerChild=>{
                        if(data.id == piggerChild.id){
                            parentLock = true
                        }else {
                            piggerChild.disabled = !piggerChild.disabled
                        }
                    })
                    if (!parentLock) {
                        pigger.disabled = !pigger.disabled
                    }
                })
            }
        }
    }
</script>

<style scoped>

</style>